﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace sort_test
{
  class Program
  {

    static String getRelativePath(String from, String to)
    {
      System.Uri uriFrom = new Uri(from);
      System.Uri uriTo = new Uri(to);
      Uri reltivePath = uriFrom.MakeRelativeUri(uriTo);
      return reltivePath.ToString();
    }

    static bool FileCompare(String file1, String file2)
    {
      bool isNotEqual = false;
      int lineNumber = 0;
      int i = 0;
      StreamReader sr1 = new StreamReader(file1);
      StreamReader sr2 = new StreamReader(file2);
      while (!sr1.EndOfStream && !sr2.EndOfStream)
      {
        i++;
        string line1 = sr1.ReadLine();
        string line2 = sr2.ReadLine();
        if (line1 != line2)
        {
          isNotEqual = true;
          lineNumber = i;
        }
      }
      if (isNotEqual)
      {
        return false;
      }
      sr1.Close();
      sr2.Close();
      return true;
    }

    static void Main(string[] args)
    {
      if (args.Length != 2)
      {
        Console.WriteLine("Please enter the directory to all projects, ant ouput directory.");
        return;
      }

      String initPath = args[1];
      //programs directory
      String currentDirectory = Directory.GetCurrentDirectory();
      //directory to projects, example: C:\course\antond
      DirectoryInfo dir = new DirectoryInfo(args[0]);
      //directory with test_files must lie near
      DirectoryInfo inputDir = new DirectoryInfo(currentDirectory + @"\test_files");
      //directory with files to check
      DirectoryInfo answerDir = new DirectoryInfo(currentDirectory + @"\answer_files");
      foreach (FileInfo file in dir.GetFiles("*.exe", SearchOption.AllDirectories))
      {
        Console.WriteLine("Checking file: " + file.FullName);

        //Creating directory, where result.txt will be
        Directory.CreateDirectory(initPath + @"\results\" + getRelativePath(dir.FullName, file.Directory.FullName));
        //Result file for every .exe file
        StreamWriter result = new StreamWriter(initPath + @"\results\" + getRelativePath(dir.FullName, file.Directory.FullName) + @"\result.txt");

        foreach (FileInfo inputFile in inputDir.GetFiles("*.txt"))
        {
          System.Diagnostics.Process proc = new System.Diagnostics.Process();
          proc.StartInfo.FileName = file.FullName;
          String outputFile = initPath + @"\outputOFtheTestedProgram.txt";
          //Workng with process
          proc.StartInfo.Arguments = inputFile.FullName + " " + outputFile;
          proc.StartInfo.UseShellExecute = false;
          proc.StartInfo.RedirectStandardOutput = true;
          proc.StartInfo.RedirectStandardError = true;
          proc.Start();

          //Errors and output
          String output = proc.StandardOutput.ReadToEnd();
          String error = proc.StandardError.ReadToEnd();

          //Information into result file
          result.WriteLine("Processing file: " + inputFile.Name);
          result.WriteLine(output);
          result.WriteLine(error);
          proc.WaitForExit();
          proc.Close();
          //Path to the answer file
          String answerFile = answerDir.FullName + @"\" + inputFile.Name.Replace("input", "output");
          //Compare
          result.WriteLine("Comparing:" + outputFile + " with " + answerFile);
          if (!File.Exists(outputFile))
            result.WriteLine("Didn't create file for input: " + inputFile.FullName);
          else if (!FileCompare(answerFile, outputFile))
            result.WriteLine("Wrong sort: " + inputFile.FullName);
        }
        result.Close();
      }
    }
  }
}